#include "qelib.h"
#include "qe_driver.h"
#include "libfdt.h"

QELOG_DOMAIN("test2");

static qe_ret test2_probe(const void *fdt, int offset)
{
    int len;
    int node_offset;
    const char *name;
    const char *spi_name;
    char spi_path[128];
    const qe_u32 *prop_spi;
    qe_notice("probe");
    name = fdt_get_name(fdt, offset, QE_NULL);
    qe_notice("name %s", name);

    prop_spi = fdt_getprop(fdt, offset, "spi-bus", &len);
    if (!prop_spi) {
        node_offset = fdt_parent_offset(fdt, offset);
        qe_notice("node offset %d", node_offset);
        qe_notice("parent %s", fdt_get_name(fdt, node_offset, QE_NULL));
        return qe_ok;
    }

    node_offset = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*prop_spi));
    qe_notice("node offset %d", node_offset);
    spi_name = fdt_get_name(fdt, node_offset, QE_NULL);
    qe_notice("spi %s", spi_name);
    return qe_ok;
}

static const qe_device_id test2_ids[] = {
	{.compatible = "analog,ad936x",},
    {}
};

QE_DRIVER(test2) = {
    .name = "test2",
    .of_match = test2_ids,
    .probe = test2_probe,
};